package br.hm.operandroid.activity;

import java.util.Random;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import br.hm.operandroid.R;
import br.hm.operandroid.service.ILabelContactOperatorService;
import br.hm.operandroid.service.IStateUpdadeLabelListener;
import br.hm.operandroid.service.LabelContactOperatorService.LabelContactOperatorBinder;

public class LabelContactOperatorActivity extends Activity implements
		ServiceConnection, IStateUpdadeLabelListener {

	private Button buttonUpdateNow;
	private ProgressBar progressBar;
	private Handler handler = new Handler();
	private ILabelContactOperatorService labelContactOperatorService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_label_operator);

		buttonUpdateNow = (Button) findViewById(R.id.labelButtonUpdateNow);
		progressBar = (ProgressBar) findViewById(R.id.labelprogressbar);

		buildButtonUpdateNow();

		Intent intent = new Intent("br.hm.operandroid.UPDATE_LABEL_SERVICE");
		getApplicationContext().bindService(intent,
				LabelContactOperatorActivity.this, Context.BIND_AUTO_CREATE);
	}

	private void buildButtonUpdateNow() {
		OnClickListener onClickListener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (labelContactOperatorService != null) {
					String label;
					if (labelContactOperatorService.isUpdatingLabel()) {
						label = getResources().getString(
								R.string.label_lblprocessupdate);
					} else {
						labelContactOperatorService
								.updateLabelContactOperator(new Random()
										.nextInt());
						label = getResources().getString(
								R.string.label_lblstartprocessupdate);
					}

					Toast.makeText(LabelContactOperatorActivity.this, label,
							Toast.LENGTH_LONG).show();
				}
			}
		};
		buttonUpdateNow.setOnClickListener(onClickListener);
	}

	@Override
	public void onServiceConnected(ComponentName name, IBinder binder) {
		labelContactOperatorService = ((LabelContactOperatorBinder) binder)
				.getLabelContactOperatorService();
		labelContactOperatorService.setOnStateUpdadeLabelListener(this);

	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		labelContactOperatorService = null;

	}

	@Override
	public void onStartUpdadeLabel(ILabelContactOperatorService service) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				progressBar.setVisibility(View.VISIBLE);
			}
		});

	}

	@Override
	public void onEndUpdadeLabel(ILabelContactOperatorService service) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				progressBar.setVisibility(View.INVISIBLE);
				Toast.makeText(
						LabelContactOperatorActivity.this,
						getResources().getString(
								R.string.label_lblendprocessupdate),
						Toast.LENGTH_LONG).show();

			}
		});
	}

	@Override
	protected void onDestroy() {
		if (labelContactOperatorService != null) {
			labelContactOperatorService.setOnStateUpdadeLabelListener(null);
		}
		super.onDestroy();
	}

}
